package com.slmxz.tms.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slmxz.tms.common.model.Query;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import com.slmxz.tms.common.support.Condition;
import com.slmxz.tms.document.model.entity.Customer;
import com.slmxz.tms.document.model.vo.CustomerVO;
import com.slmxz.tms.document.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 客户前端控制层
 * @author sunyao
 * @Date 2024-11-15
 */
@Tag(name = "客户接口")
@RestController
@RequestMapping("/document/customer")
@RequiredArgsConstructor
public class CustomerController  {

    private final CustomerService customerService;

    @Operation(summary = "客户分页列表")
    @GetMapping("/page")
    public PageResult<CustomerVO> selectCustomerPage(Query query, CustomerVO customer) {
        IPage<CustomerVO> result = this.customerService
                .selectCustomerPage(Condition.getPage(query),customer);
        return PageResult.success(result);
    }


    @Operation(summary = "客户信息的下拉框")
    @GetMapping("/selectOption")
    public Result<?> selectOption(CustomerVO customer){
        return Result.success(this.customerService.selectOption(customer));
    }


    @Operation(summary = "新增客户")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('document:customer:add')")
    public Result<Void> saveCustomer(@RequestBody @Valid CustomerVO  customer) {
        boolean result = this.customerService.saveCustomer(customer);
        return Result.result(result);
    }

    @Operation(summary = "获取客户详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('document:customer:edit')")
    public Result<Customer> selectCustomerDetail(
        @Parameter(description = "客户ID") @PathVariable Long id
    ) {
        Customer customer = this.customerService.selectCustomerDetail(id);
        return Result.success(customer);
    }

    @Operation(summary = "修改客户")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('document:customer:edit')")
    public Result<Void> updateCustomer(
            @RequestBody @Validated CustomerVO customer
    ) {
        boolean result = customerService.updateCustomer(customer);
        return Result.result(result);
    }

    @Operation(summary = "删除客户")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('document:customer:delete')")
    public Result<Void> deleteCustomers(
        @Parameter(description = "客户ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.customerService.deleteCustomers(ids);
        return Result.result(result);
    }

    @Operation(summary = "激活或者注销客户")
    @PostMapping("/activeOrLogout")
    @PreAuthorize("@ss.hasPerm('document:business:delete')")
    public Result<Void> activeOrLogout(@RequestBody CustomerVO customer) {
        boolean result = this.customerService.activeOrLogout(customer);
        return Result.result(result);
    }
}
